<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="原价" prop="oldPrice">
        <el-input v-model="ruleForm.oldPrice"></el-input>
      </el-form-item>
      <el-form-item label="销量" prop="saleCount">
        <el-input v-model="ruleForm.saleCount"></el-input>
      </el-form-item>
      <el-form-item label="库存" prop="num">
        <el-input v-model="ruleForm.num"></el-input>
      </el-form-item>
      <el-form-item label="商品类编号" prop="proCategoryId">
        <el-input v-model="ruleForm.proCategoryId"></el-input>
      </el-form-item>
      <el-form-item label="图片地址" prop="urls">
        <el-input v-model="ruleForm.urls"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

export default {
  data() {
    return {
      ruleForm: {
        title: '测试商品111',
        price: '测试商品价格222',
        oldPrice: '测试商品价格333',
        saleCount: '测试商品原价444',
        num: '测试商品库存555',
        proCategoryId: '测试商品类编号666',
        urls: '测试商品图片地址777',
      },
      rules: {
        title: [
          {required: true, message: '请输入商品标题', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '请输入商品价格', trigger: 'blur'}
        ],
        oldPrice: [
          {required: true, message: '请输入商品原价', trigger: 'blur'}
        ],
        saleCount: [
          {required: true, message: '请输入商品销量', trigger: 'blur'}
        ],
        num: [
          {required: true, message: '请输入商品类库存', trigger: 'blur'}
        ],
        proCategoryId: [
          {required: true, message: '请输入商品类编号', trigger: 'blur'}
        ],
        urls: [
          {required: true, message: '请输入商品图片地址', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9999/products/add-new';
          console.log('url= ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData= ' + formData);
          this.axios.post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '添加商品成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$alert(jsonResult.message, '操作失败', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>